@file:Suppress("UNREACHABLE_CODE")

package com.et.drive.info

import android.annotation.SuppressLint
import com.et.drive.run.Run
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.convert.ILightTemperature
import com.et.machine.convert.lightTemperatureInfo
import com.et.protocol.ano.Guide2Code
import com.et.util.insertByte
import com.et.util.toBCDByte
import com.et.util.toBCDInt
import com.et.util.toByteArray
import java.text.SimpleDateFormat
import java.util.*


data class LightTemperatureInfo(
    var deviceNumber: Int,
    var salesConfig: Int,
    var lightStatus: LightStatusEnum,
    var openLightTime: Time,
    var leftRoomStatus: TemperatureStateEnum,
    var rightRoomStatus: TemperatureStateEnum,
    var energySavingTime: Time,
    var settingCoolTemperature: Int,
    var actualCoolTemperature: Int,
    var settingHotTemperature: Int,
    var actualHotTemperature: Int,
) {
    override fun toString(): String {
        return "LightTemperatureInfo(" +
                "deviceNumber='$deviceNumber'," +
                "salesConfig='$salesConfig'," +
                "lightStatus='$lightStatus'," +
                "openLightTime='$openLightTime'," +
                "leftRoomStatus='$leftRoomStatus'," +
                "rightRoomStatus='$rightRoomStatus'," +
                "energySavingTime='$energySavingTime'," +
                "settingCoolTemperature='$settingCoolTemperature'," +
                "actualCoolTemperature='$actualCoolTemperature'," +
                "settingHotTemperature='$settingHotTemperature'," +
                "actualHotTemperature='$actualHotTemperature')"
    }
}

object ILightTemperatureImpl : ILightTemperature {
    private var callback: ((LightTemperatureInfo) -> Unit?)? = null
    override fun subscribe(_callback: (LightTemperatureInfo) -> Unit) {
        callback = _callback
    }

    fun lightTemperatureInfoNotify(byteArray: ByteArray) {
        callback?.let {
            it(
                LightTemperatureInfo(
                    deviceNumber = byteArray.toBCDInt(0, 1),
                    salesConfig = byteArray.toBCDInt(1, 1),
                    lightStatus = when (byteArray.toBCDInt(2, 1)) {
                        0 -> LightStatusEnum.Auto
                        1 -> LightStatusEnum.AlwaysClose
                        2 -> LightStatusEnum.AlwaysOpen
                        else -> LightStatusEnum.Auto
                    },
                    openLightTime = Time(
                        byteArray.toBCDInt(3, 1),
                        byteArray.toBCDInt(4, 1),
                        byteArray.toBCDInt(5, 1),
                        byteArray.toBCDInt(6, 1)
                    ),
                    leftRoomStatus = when (byteArray[7].toInt()) {
                        0 -> TemperatureStateEnum.Cool
                        1 -> TemperatureStateEnum.Hot
                        2 -> TemperatureStateEnum.Disable
                        else -> TemperatureStateEnum.Disable
                    },
                    rightRoomStatus = when (byteArray[7].toInt()) {
                        0 -> TemperatureStateEnum.Cool
                        1 -> TemperatureStateEnum.Hot
                        2 -> TemperatureStateEnum.Disable
                        else -> TemperatureStateEnum.Disable
                    },
                    energySavingTime = Time(
                        byteArray.toBCDInt(9, 1),
                        byteArray.toBCDInt(10, 1),
                        byteArray.toBCDInt(11, 1),
                        byteArray.toBCDInt(12, 1)
                    ),
                    settingCoolTemperature = byteArray.toBCDInt(13, 1),
                    actualCoolTemperature = byteArray.toBCDInt(14, 1),
                    settingHotTemperature = byteArray.toBCDInt(15, 1),
                    actualHotTemperature = byteArray.toBCDInt(16, 1)
                ).also {
                    lightTemperatureInfo[it.deviceNumber] = it
                    ELog.d(TAGS.machine, "Light and Temperature Info $it")
                }
            )
        }
    }
}

enum class LightStatusEnum {
    Auto, AlwaysClose, AlwaysOpen
}

enum class TemperatureStateEnum {
    Cool, Hot, Disable
}

data class Time(
    var startHour: Int,
    var startMin: Int,
    var endHour: Int,
    var endMin: Int,
)


